<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<script th:src="@{/static/js/jquery.min.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/static/js/afquery.js}" type="text/javascript" charset="utf-8"></script>
		<script th:src="@{/static/layui/layui.all.js}" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" type="text/css" th:href="@{/static/layui/css/layui.css}" />
		<link rel="stylesheet" type="text/css" th:href="@{/static/css/Common.css}" />
	</head>
	<body>
		
		<!-- 对话框面板定义 -->
		<div class='clazz_dlg_define' style='display:none'>
			<div class='pane' style='padding:10px'>
				<div style='padding:10px'>
					<span>选择学院:</span>
					 <select class="academyID" >
						<option th:each ="row : ${AcademyList}" th:text="${row.AcademyName}" th:value="${row.academyId}"></option>
					 </select>
				</div>
				<div style='padding:10px'>
					<span>新增班级:</span>
					<input type='text' style='width:100%' placeholder="请输入班级号">
				</div>
				<div style='padding:10px'>
					<span>新增辅导员:</span>
					<input type='text' style='width:100%' placeholder="请输入教师职工号">
				</div>
			</div>
		</div>
		
		<!-- 对话框面板定义 -->
		<div class='academy_dlg_define' style='display:none'>
			<div class='pane' style='padding:10px'>
				<div style='padding:10px'>
					<span>新增学院:</span>
					 <input type='text' style='width:100%' placeholder="请输入学院名">
				</div>
				<div style='padding:10px'>
					<span>新增学院代号:</span>
					<input type='text' style='width:100%' placeholder="请输入学院代号">
				</div>
				<div style='padding:10px'>
					<span>新增院长:</span>
					<input type='text' style='width:100%' placeholder="请输入职工号">
				</div>
			</div>
		</div>
		<div class="layui-row" id="Main" style="height: 100%;overflow: hidden;">
			<!-- 顶部区 -->
			<div th:replace =" ~{A_Common::Top_Menu}"></div>
			
			<!-- 左侧菜单区 -->
			<div th:replace =" ~{A_Common::Left_Menu}"></div>
			
			<!-- 右侧内容区 -->
			<!-- =============================================班级管理================================================ -->
			
			<div th:fragment="Clazz_Manager" class="layui-col-xs8 layui-col-md10 layui-row" id="right_Content">
				<fieldset class="layui-elem-field site-demo-button" style="padding: 5%;">
					<legend>班级管理</legend>
					  <div class="layui-tab">
					  	<ul class="layui-tab-title">
					  		<li onclick="M.tab(this)">学院管理</li>
					  		<li onclick="M.tab(this)">班级管理</li>
					  	</ul>
					  	<div class="layui-tab-content">
					  		<div class="layui-tab-item" id="academyTable">
					  			<table class="layui-table">
					  				<colgroup>
					  					<col>
					  				</colgroup>
					  				<thead>
					  					<tr>
					  						<th><input type="checkbox" onclick="M.SelectAll(this)" /></th>
					  						<th>序号</th>
					  						<th>学院</th>
					  						<th>学院编号</th>
											<th>院长</th>
											<th>职工号</th>
					  					</tr>
					  				</thead>
					  				<tbody>
										<tr th:each = "row : ${academyList}">
											<td><input type="checkbox" name="selectID"/></td>
											<td th:idd="${row.id}" th:text="${rowStat.index+1}">1</td>
											<td th:text="${row.academyName}"> 软件学院</td>
											<td th:text="${row.academyId}"> 005</td>
											<td th:text="待定" style="background-color: peru;color: white;"> 李老师</td>
											<td th:text="待定" style="background-color: peru;color: white;"> 653005</td>
										</tr>
					  					<tr th:each = "row : ${A_TeacherList}">
					  						<td><input type="checkbox" name="selectID"/></td>
					  						<td th:idd="${row.id}" th:text="${rowStat.index+1}">1</td>
					  						<td th:text="${row.AcademyName}"> 软件学院</td>
					  						<td th:text="${row.AcademyId}"> 005</td>
											<td th:text="${row.RealName}"> 李老师</td>
											<td th:old_Dean="${row.Dean}" th:text="${row.Dean}"> 653005</td>
					  					</tr>
										
									
					  				</tbody>
					  			</table>
					  			<div class="layui-row">
					  				<div class="layui-btn-group layui-col-md-offset8">
										<button type="button" class="layui-btn" onclick="openAddAcademy()">新增</button>
					  					<button type="button" class="layui-btn" onclick="M.OpenUpdate(this)">编辑</button>
					  					<button type="button" class="layui-btn" onclick="M.SaveUpdate(this)">保存</button>
					  				</div>
					  			</div>
					  		</div>
					  		<div class="layui-tab-item" id="clazzTable">
					  			<table class="layui-table">
					  				<colgroup>
					  					<col>
					  				</colgroup>
					  				<thead>
					  					<tr>
					  						<th><input type="checkbox" onclick="M.SelectAll(this)" /></th>
					  						<th>序号</th>
					  						<th>班级</th>
											<th>辅导员</th>
											<th>工号</th>
					  						<th>学院</th>
											<th>学院编号</th>
					  					</tr>
					  				</thead>
					  				<tbody>
					  					<tr th:each = "row : ${A_T_ClazzList}">
					  						<td><input type="checkbox" name="selectID" /></td>
					  						<td th:idd="${row.id}" th:text="${rowStat.index+1}">1</td>
					  						<td th:text="${row.ClazzId}">1790004</td>
					  						<td th:text="${row.RealName}">李老师</td>
											<td th:text="${row.Instructor}">536005</td>
											<td>
												<select class="academyID" disabled="disabled" >
													<option th:each ="rows : ${AcademyList}" th:text="${rows.academyName}"
													 th:value="${rows.academyId}" th:selected="${rows.academyName == row.AcademyName}"></option>
												</select>
											</td>
											<td th:text="${row.AcademyId}">233</td>
					  					</tr>
					  				</tbody>
					  			</table>
					  			<div class="layui-row">
					  				<div class="layui-btn-group layui-col-md-offset7">
										<button type="button" class="layui-btn" onclick="openAddClazz()">新增</button>
					  					<button type="button" class="layui-btn" onclick="M.OpenUpdate(this)">编辑</button>
					  					<button type="button" class="layui-btn" onclick="M.SaveUpdate(this)">保存</button>
					  				</div>
					  			</div>
					  		</div>
					  	</div>
				</fieldset>
			</div>
			
		</div>
	</body>
	<!-- 初始化点击事件 -->
	<script th:replace =" ~{A_Common::Init_Click}"></script>
	<script th:inline="javascript">
		var TeacherList = [[${TeacherList}]];
		var AcademyList = [[${AcademyList}]];
		var ClazzList = [[${ClazzList}]];
		// var M = {};
		function openAddClazz(){
					
					var dlgContent = $('.clazz_dlg_define').html(); // 窗口内容
					
					layer.open({
						type: 1,
						title: '添加记录', 
						area: ['420px', 'auto'], 
						content: dlgContent,
						btn:['确定','放弃'] ,
						
						btn1: function(index,layero){
							let isExist = false;
							let req = {};
							req.academyId =Number($(".pane select option:selected",layero).val().trim());
							req.clazzId =Number($(".pane input:eq(0)",layero).val().trim());
							req.teacherId =Number($(".pane input:eq(1)",layero).val().trim());
							
							// for(let i = 0;i<ClazzList.length;i++){
							// 	// if(req.clazzId == ClazzList[i]){
									
							// 	// }
							// }
							
							Af.rest([[@{clazz_savaClazz.do}]],req,function(data){
								layer.open({
									title: "信息",
									content: '成功！',
									offset: 't',
									time: 500,
									success: function(layero, index){
										setTimeout(function(){location.reload();},600);
									  }
								});
							});
							
							// 关闭对话框
							layer.close(index)	;				
						},
						btn2 : function(index,layero){
							layer.close(index)		
						}
					});	
					
				}
				
		function openAddAcademy(){
					
					var dlgContent = $('.academy_dlg_define').html(); // 窗口内容
					
					layer.open({
						type: 1,
						title: '添加记录', 
						area: ['420px', 'auto'], 
						content: dlgContent,
						btn:['确定','放弃'] ,
						
						btn1: function(index,layero){
							let isExist = false;
							let req = {};
							req.academyName = $(".pane input:eq(0)",layero).val().trim();
							req.academyId =Number($(".pane input:eq(1)",layero).val().trim());
							req.teacherId =Number($(".pane input:eq(2)",layero).val().trim());
							
							for(let i=0;i<AcademyList.length;i++){
								if(req.academyId == AcademyList[i].AcademyId || req.academyName == AcademyList[i].academyName){
									isExist = true;
									break;
								}
							}
							if(isExist){
								layer.open({
									title: "信息",
									content: '已存在同名的学院或编号！',
									offset: 't',
									time: 2000
								});
								return;
							}
							isExist = true;
							for(let i=0;i<TeacherList.length;i++){
								if(req.teacherId == TeacherList[i].teacherId){
									isExist = false;
									break;
								}
							}
							if(isExist){
								layer.open({
									title: "信息",
									content: '系统中不存在此职工！',
									offset: 't',
									time: 2000
								});
								return;
							}
							Af.rest([[@{clazz_saveAcademy.do}]],req,function(data){
								layer.open({
									title: "信息",
									content: '成功！',
									offset: 't',
									time: 500,
									success: function(layero, index){
										setTimeout(function(){location.reload();},600);
									  }
								});
							});
							// 关闭对话框
							layer.close(index)	;				
						},
						btn2 : function(index,layero){
							layer.close(index)		
						}
					});	
					
				}
		
	//Demo
	layui.use('form', function(){
	  var form = layui.form;
	  form.render();
	  //监听提交
	  form.on('submit(formDemo)', function(data){
	    layer.msg(JSON.stringify(data.field));
	    return false;
	  });
	  
	});
	
	// 删除一条记录
	M.DeleteModel = function(e){
		let tables = $($(e).parent().parent()).prev();
		let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
		if (checkeds.length > 0) {
			let datas = new Array();
			for (let i = 0; i < checkeds.length; i++) {
				let s = $($(checkeds.get(i)).parent()).next().attr("idd");
				
				datas.push(Number(s));
			}
			
			Af.rest([[@{clazz_deleteClazz.do}]],datas,function(data){
				layer.open({
					title: "信息",
					content: '成功！',
					offset: 't',
					time: 500,
					success: function(layero, index){
						setTimeout(function(){location.reload();},600);
					  }
				});
			});
			
		}
	}
	
	//修改信息
	var count = 0;
	M.OpenUpdate = function(e) {
	
		let tables = $($(e).parent().parent()).prev();
		let id = $($(e).parent().parent().parent()).attr("id");
		let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
		count = checkeds.length;
		if (checkeds.length > 0) {
			$(checkeds.parent().parent()).css("background-color","khaki");
			$($(checkeds).parent().parent()).attr("contenteditable", true);
			let trs = $(checkeds).parent().parent();
			for (let i = 0; i < checkeds.length; i++) {
				$($($(trs.get(i)).children()).get(0)).attr("contenteditable", false);
				$($($(trs.get(i)).children()).get(1)).attr("contenteditable", false);
				$($($(trs.get(i)).children()).get(4)).attr("contenteditable", false);
				$($(trs.get(i)).find("td select")).prop("disabled",false);
			}
			if(id == "clazzTable"){
				$($($(trs).children()).get(3)).attr("contenteditable", false);
				$($($(trs).children()).get(4)).attr("contenteditable", true);
				$($($(trs).children()).get(5)).attr("contenteditable", false);
				$($($(trs).children()).get(6)).attr("contenteditable", false);
			}
		}else{
			layer.open({
				title: "信息",
				content: '请选择需要修改的数据！',
				offset: 't',
				time: 2000
			});
		}
	}
	
	//保存修改后信息
	M.SaveUpdate = function(e) {
	
		let tables = $($(e).parent().parent()).prev();
		let trs = $(tables).find("tbody tr");
		
		let checkeds = $($(tables).find("tbody tr td input[name='selectID']:checked"));
		let datas = new Array();
		if (checkeds.length != count) {
			layer.open({
				title: "信息",
				content: '请检查修改的数据数量！',
				offset: 't',
				time: 2000
			});
		} else {
			let tableId = $($(e).parent().parent().parent()).attr("id");
			if(tableId == "academyTable"){
				for (let i = 0; i < checkeds.length; i++) {
					var req = {};
					req.id = Number($($($($(checkeds[i]).parent().parent()).children()).get(1)).attr("idd"));
					req.AcademyName = $($($($(checkeds[i]).parent().parent()).children()).get(2)).text().trim();
					req.AcademyId = $($($($(checkeds[i]).parent().parent()).children()).get(3)).text().trim();
					req.Dean = $($($($(checkeds[i]).parent().parent()).children()).get(5)).text().trim();
					req.oldDean = Number($($($($(checkeds[i]).parent().parent()).children()).get(5)).attr("old_Dean"));
					datas.push(req);
					}
					Af.rest([[@{clazz_savaUpdateAcademy.do}]], datas, function(data) {
						layer.open({
							title: "信息",
							content: '成功！',
							offset: 't',
							time: 500,
							success: function(layero, index){
								setTimeout(function(){location.reload();},600);
							  }
						});
					});
				}else{
					for (let i = 0; i < checkeds.length; i++) {
						var req = {};
						req.id = Number($($($($(checkeds[i]).parent().parent()).children()).get(1)).attr("idd"));
						req.ClazzId =Number( $($($($(checkeds[i]).parent().parent()).children()).get(2)).text().trim());
						req.AcademyId =Number($($(checkeds[i]).parent().parent()).find("select option:selected").val().trim());
						req.Instructor =Number($($($($(checkeds[i]).parent().parent()).children()).get(4)).text().trim());
						
						datas.push(req);
					}
					Af.rest([[@{clazz_savaUpdateClazz.do}]], datas, function(data) {
						layer.open({
							title: "信息",
							content: '成功！',
							offset: 't',
							time: 500,
							success: function(layero, index){
								setTimeout(function(){location.reload();},600);
							  }
						});
					});
				}
				$(trs).attr("contenteditable", false);
				$(trs.find("td select")).prop("disabled",true);
				$($(tables).find("tbody tr td input")).prop("checked", false);
				$(checkeds.parent().parent()).css("background-color","white");
				count = 0;
			}
	}
	
	var page = sessionStorage.getItem("page");
	M.init = function(){
		$(tab_titles).removeAttr("class");
		$(tab_contents).hide();
		if(Number(page) >= 0){
			$(tab_titles).removeAttr("class");
			$(tab_contents).hide();
			
			switch(Number(page)){
				case 0:
					sessionStorage.setItem("page",0);
					$(".layui-tab-title li:eq(0)").attr("class","layui-this");
					$(".layui-tab-content .layui-tab-item:eq(0)").show();
				break;
				case 1:
					sessionStorage.setItem("page",1);
					var s = $(".layui-tab-title li:eq(1)").attr("class","layui-this");
					var m = $(".layui-tab-content .layui-tab-item:eq(1)").show();
				break;
				default: 
				       sessionStorage.setItem("page",0);
				       $(".layui-tab-title li:eq(0)").attr("class","layui-this");
				       $(".layui-tab-content .layui-tab-item:eq(0)").show();
				break;
			}	
		}else{
			sessionStorage.setItem("page",0);
			$(".layui-tab-title li:eq(0)").attr("class","layui-this");
			$(".layui-tab-content .layui-tab-item:eq(0)").show();
			}
		}();
	
	</script>
	
	
</html>
